package com.fdsc.erp.weixin.Menu;

import com.fdsc.erp.weixin.Utils.CommonUtil;
import net.sf.json.JSONObject;

/**
 * @Description:自定义菜单工具类
 * @Author:lishanhui
 * @Date:Create in 16:36  2018-06-06
 **/
public class MenuUtil {
    //微信创建菜单接口地址
    public static String MENUURL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
    //微信查询菜单接口地址
    public static String SELECTMENUURL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";
    //微信删除菜单接口地址
    public static String DELECTMENUURL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

    /**
     * @Date:Create in 14:32 2018-06-06
     * @Description:查询菜单
     **/
    public static String delectMenu(String accessToken) {
        String result = null;
        String url = DELECTMENUURL.replace("ACCESS_TOKEN", accessToken);
        try {
            result = CommonUtil.doGet(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Date:Create in 14:32 2018-06-06
     * @Description:查询菜单
     **/
    public static String selectMenu(String accessToken) {
        String result = null;
        String url = SELECTMENUURL.replace("ACCESS_TOKEN", accessToken);
        try {
             result = CommonUtil.doGet(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Date:Create in 14:31 2018-06-06
     * @Description:创建菜单
     **/
    public static boolean makeMenu(Menu menu, String accessToken) {
        boolean result = false;
        String url = MENUURL.replace("ACCESS_TOKEN", accessToken);
        //将菜单对象转换JSON字符串
        String jsonMenu = JSONObject.fromObject(menu).toString();
        //提交服务器
        try {
            String respone = CommonUtil.doPost(url, jsonMenu);
            JSONObject object = JSONObject.fromObject(respone);
            //{"errcode":0,"errmsg":"ok"}
            int code = object.getInt("errcode");
            String meg = object.getString("errmsg");
            if (0 == code) {
                result = true;
            } else {
                result = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @Date:Create in 13:21 2018-06-06
     * @Description:创建菜单xml数据
     * @params: * @param null
     * @return:
     **/
    private static String makeMenuXml() {
        ClickButton btn1 = new ClickButton();
        btn1.setName("开锁");
        btn1.setType("click");
        btn1.setKey("V1001_0");

        ViewButton btn2 = new ViewButton();
        btn2.setName("简介");
        btn2.setType("view");
        btn2.setUrl("http://www.qq.com");

        ClickButton btn31 = new ClickButton();
        btn31.setName("Hello World");
        btn31.setType("click");
        btn31.setKey("V1001_1");

        ClickButton btn32 = new ClickButton();
        btn32.setName("赞");
        btn32.setType("click");
        btn32.setKey("V1001_2");

        ComplexButton btn3 = new ComplexButton();
        btn3.setName("菜单");
        btn3.setSub_button(new Button[]{btn31, btn32});

        //创建菜单对象
        Menu menu = new Menu();
        menu.setButton(new Button[]{btn1, btn2, btn3});

        //将菜单对象转换JSON字符串
        String jsonMenu = JSONObject.fromObject(menu).toString();
        System.out.println(jsonMenu);
        return jsonMenu;
    }
}
